#include <iostream>
using namespace std;

// Pointer examples in C++
int fun_p()
{
    // 1. Basic pointer usage
    int a = 10;
    int *p = &a; // p stores the address of a
    cout << "a : " << a << endl;
    cout << "Address of a: " << &a << endl;
    cout << "Pointer p points to: " << p << endl;
    cout << "Value via pointer *p: " << *p << endl; // Dereference pointer

    // 2. Modify value via pointer
    *p = 20; // Change the value of a through pointer
    cout << "Updated a: " << a << endl;

    // 3. Pointer with arrays
    int arr[3] = {1, 2, 3};
    int *pArr = arr; // Points to the first element of the array

    cout << "arr[0]: " << *pArr << endl;       // Access first element
    cout << "arr[1]: " << *(pArr + 1) << endl; // Access second element
    cout << "arr[2]: " << *(pArr + 2) << endl; // Access third element

    // 4. Dynamic memory allocation
    int *pNew = new int(42); // Allocate an int on the heap
    cout << "Dynamic int: " << *pNew << endl;
    delete pNew; // Free the allocated memory

    return 0;
}

int main()
{
    fun_p();
    return 0;
}
